import {Injectable} from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {API} from "../../../constants/constants";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";
import {FormUtil} from "../../../util/form-util";
import {Site} from "./site";

@Injectable()
export class SitesService {

  site :Site;

  constructor(private requestUtil: RequestUtil,
              private formUtil: FormUtil) {
  }

  insertSite(site: Site): Observable<MsgResult> {
    let params = this.formUtil.serialize(site);
    return this.requestUtil.post(API.insertSite, params);
  }

  deleteSite(id: string,siteCode :string): Observable<MsgResult> {
    let params = "id=" + id + "&siteCode=" + siteCode;
    return this.requestUtil.get(API.deleteSite, params);
  }

  delSites(ids: Array<string>) {
    let params = "idStr=" + ids;
    return this.requestUtil.post(API.delSites, params);
  }

  getSiteById(id: string) {
    let params = "id=" + id;
    return this.requestUtil.get(API.getSiteById, params)
  }

  updateSite(site: Site): Observable<MsgResult> {
    let params = this.formUtil.serialize(site);
    return this.requestUtil.post(API.updateSite, params);
  }

  /**
   * 获取行政区划关联树
   * @param userCode
   * @returns {Observable<MsgResult>|any}
   */
  getRetrieveRegionsTree(regionCode: string): Observable<MsgResult> {
    let params = "regionCode=" + regionCode;
    return this.requestUtil.get(API.getRetrieveRegionsTree, params);
  }

  getSiteCategoryInfos(): Observable<MsgResult> {
    let params = "isUse=" + "1";
    return this.requestUtil.get(API.getSiteCategoryInfos, params);
  }

  getStandardSite(regionId,siteName): Observable<MsgResult>{
    let params = "pId=" + "01"+"&regionId="+regionId+"&siteName="+siteName;
    return this.requestUtil.post(API.getSiteBySiteCategory, params);
  }

  /**
   *
   * @returns {Observable<MsgResult>|any}
   */
  getGovInfos(param:string): Observable<MsgResult>{
    let params = "name=" + param;
    return this.requestUtil.get(API.getGovInfos,params);
  }

  /**
   *
   * @returns {Observable<MsgResult>|any}
   */
  getEntInfos(param:string): Observable<MsgResult>{
    let params = "name=" + param;
    return this.requestUtil.get(API.getEntInfos,params);
  }


}
